package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.business.constants.CommonConstant;
import com.dmcb.common.business.services.BaseService;
import com.dmcb.common.business.utils.JsonUtil;
import com.dmcb.common.business.utils.WebUtil;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.entities.Article;
import com.dmcb.trade.business.mappers.ArticleMonitorMapper;
import com.dmcb.trade.controller.ArticleCrawlerController;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 外滩文章导入，直接导入到监测表
 * Created by 黑米 on 2016/12/13.
 */
@Service
public class WaiTanCrawler extends BaseService {
    Logger logger = LoggerFactory.getLogger(JianshuCrawler.class);

    @Autowired
    private ArticleCrawlerController articleCrawlerController;
    @Autowired
    private ArticleMonitorMapper articleMonitorMapper;


    public JsonResult crawl() {
        // 爬取文章
        int totalAll = 0;
        String link = "http://www.bundpic.com/postpage?n=12&p=";
        for (int i = 1; true; i++) {
            String json = WebUtil.buildRequest(link + i,false);
            if (StringUtils.isBlank(json)) {
                break;
            }
            JSONArray jsonArray = JsonUtil.readJsonArray(json);
            JsonResult result = iterate(2512, jsonArray);
            if (!result.isSuccess()) {
                return result;
            }
            totalAll += result.getInt();
            if (result.getInt() < 1) {
                break;
            }
        }
        return totalAll == 0 ? error(CrawlConstant.MSG_WARN_NO_UPDATE) : success(totalAll);
    }

    /**
     * 迭代解析、保存文章
     *
     * @return 成功保存数量
     */
    public JsonResult iterate(int authorId, JSONArray jsonArray) {
        int succ = 0;
        int exist = 0;
        int er = 0;
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            if (!jsonObject.containsKey("标题") || !jsonObject.containsKey("链接")) {
                er++;
                continue;
            }
            // 获取标题、链接元素
            String title = WebUtil.URLDecode(jsonObject.getString("标题").trim());
            if (StringUtils.isBlank(title)) {
                continue;
            }
            try {
                // 校验作者、标题是否已存在
                Article article = articleMonitorMapper.queryByUserTitle(authorId, title);
                if (article != null) {
                    exist++;
                    if (exist >= 6 && succ == 0) {
                        return success(succ);
                    }
                    continue;
                }
                String href = jsonObject.getString("链接");
                if(StringUtils.isBlank(href)){
                   href= "http://www.bundpic.com/posts/post/"+jsonObject.getString("_id");;
                }else{
                    Thread.sleep(10000);
                }
                JsonResult result = articleCrawlerController.crawlMonitor(authorId, href);
                succ += result.isSuccess() ? 1 : 0;
            } catch (Exception e) {
                logger.error("Parser.parse error,{}", title, e);
                return error(CommonConstant.ABNORMAL_MSG);
            }
        }
        return success(succ);
    }


}

